/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.widget;

import java.util.ArrayList;

import utsc.apps.utschedule.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * This custom layout widget holds a list of {@link ObjectButtonView}.
 * @author Wenbin
 *
 */
public class ButtonsLayout extends ViewGroup {

	private ArrayList<Object> _items;
	private static final int _defaultPadding=10;
	private Button _btnAdd;
	private OnClickListener _onObjectButtonClickedListener;
	private OnLongClickListener _onObjectButtonLongClickListener;
	private OnClickListener _onAddNewButtonClickListener;
	//private boolean _refreshView=false;
	private int _columns=3;
	private boolean _requiredAdd=true;
	private int _objectBtnLayout=R.drawable.btn;
	
	public ButtonsLayout(Context context) {
		this(context,null);
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int	count=getChildCount()+(_btnAdd==null&&_requiredAdd?1:0);
		int defaultChildHeight=ObjectButtonView.MAX_HEIGHT;
		int totalRows=(int) (Math.ceil((double)count/_columns));
			
		int computedHeight=totalRows*defaultChildHeight;
		if (computedHeight<getHeight()) {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}else {
			setMeasuredDimension(resolveSize(getWidth(), widthMeasureSpec), resolveSize(computedHeight, heightMeasureSpec));
		}
		
	}
	
	/**
	 * Default constructor
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public ButtonsLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ObjectBtnLayout,
                defStyle, 0);
		_columns=a.getInteger(R.styleable.ObjectBtnLayout_btnColumns, _columns);
		_objectBtnLayout=a.getResourceId(R.styleable.ObjectBtnLayout_btnObjectBgLayout, _objectBtnLayout);
		_requiredAdd=a.getBoolean(R.styleable.ObjectBtnLayout_btnAddRequired, _requiredAdd);
	}
	public ButtonsLayout(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}
	@Override
	protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
		
		final int width=getWidth();
		final int childCount=getChildCount();
		int row=0;
		int col=0;
		
		int top=0;
		int left=0;
		int right=0;
		int bottom=0;
		int objWidth=(width-_defaultPadding)/_columns;
		int objHeight=ObjectButtonView.MAX_HEIGHT;
		int spaceOffset=(width-objWidth*_columns)/4;
		
		for (int i = 0; i < childCount; i++) {
			
			View v=getChildAt(i);
			
			top=objHeight*row+5;
			bottom=top+objHeight-5;
			left=objWidth*col+spaceOffset;
			right=left+objWidth-spaceOffset;
			v.measure(MeasureSpec.makeMeasureSpec(right-left, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(bottom-top, MeasureSpec.EXACTLY));
			
			if (v instanceof ObjectButtonView) {
				if (width-right<objWidth) {
					row++;
					col=0;
				}else {
					col++;
				}
				v.layout(left, top, right, bottom);
			}
		}
		
		if (_requiredAdd) {
			if ((_btnAdd==null||findViewById(R.id.btn_add_new_course)==null)) {
				_btnAdd=new Button(getContext());
				_btnAdd.setText("+");
				_btnAdd.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_dark));
				_btnAdd.setTextColor(Color.WHITE);
				_btnAdd.setId(R.id.btn_add_new_course);
				addView(_btnAdd, childCount);
			}
			
			top=objHeight*row+5;
			bottom=top+objHeight-5;
			left=objWidth*col+spaceOffset;
			right=left+objWidth-spaceOffset;
			_btnAdd.layout(left, top, right, bottom);
		}
		
		
	}
	
	
	public void set_items(ArrayList<Object> _items) {
		this._items = _items;
		removeAllViews();
		Context context=getContext();
		for (Object object : _items) {
			addView(new ObjectButtonView(context, object,_objectBtnLayout));
		}
	}
	
	public void add_item(Object object){
		if (_items==null) {
			_items=new ArrayList<Object>();
		}
		
		if (object!=null) {
			_items.add(object);
			addView(new ObjectButtonView(getContext(), object,_objectBtnLayout));
			
		}
	}
	
	@Override
	public void addView(View child) {
		if (child instanceof ObjectButtonView) {
			child.setOnClickListener(_onObjectButtonClickedListener);
			child.setOnLongClickListener(_onObjectButtonLongClickListener);
			super.addView(child);
		}else {
			if (child.getId()==R.id.btn_add_new_course) {
				child.setOnClickListener(_onAddNewButtonClickListener);
				super.addView(child);
			}
		}
	}
	
	@Override
	public void addView(View child, int index) {
		if (child instanceof ObjectButtonView) {
			child.setOnClickListener(_onObjectButtonClickedListener);
			child.setOnLongClickListener(_onObjectButtonLongClickListener);
			super.addView(child,index);
		}else {
			if (child.getId()==R.id.btn_add_new_course) {
				child.setOnClickListener(_onAddNewButtonClickListener);
				super.addView(child,index);
			}
		}
	}
	
	public ArrayList<Object> get_items() {
		return _items;
	}

	public OnClickListener get_onAddNewButtonClickListener() {
		return _onAddNewButtonClickListener;
	}
	
	public void set_onAddNewButtonClickListener(
			OnClickListener _onAddNewButtonClickListener) {
		this._onAddNewButtonClickListener = _onAddNewButtonClickListener;
	}
	
	public OnClickListener get_onObjectButtonClickedListener() {
		return _onObjectButtonClickedListener;
	}
	
	public void set_onObjectButtonClickedListener(
			OnClickListener _onObjectButtonClickedListener) {
		this._onObjectButtonClickedListener = _onObjectButtonClickedListener;
	}
	
	public OnLongClickListener get_onObjectButtonLongClickListener() {
		return _onObjectButtonLongClickListener;
	}
	
	public void set_onObjectButtonLongClickListener(
			OnLongClickListener _onObjectButtonLongClickListener) {
		this._onObjectButtonLongClickListener = _onObjectButtonLongClickListener;
	}
}
